package com.kdtech.analyse.news;
import com.kdtech.analyse.AnalyseNews;
import com.kdtech.utils.HtmlCleaner;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.crawler.CrawlHTML;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.DoMainUtils;
import com.kdtech.utils.HtmlCleaner;
import com.kdtech.utils.StringUtils;

/**
 *  http://news.cnpc.com.cn/  中国石油新闻中心
 *  xiaonie 2012-11-22
 */
public class CnpcNewsAnalyse implements AnalyseNews{
	private final String[] regexs={ "http://news.cnpc.com.cn/system/[0-9]{4}/[0-9]{2}/[0-9]{2}/[0-9]*.shtml"};

	
	public boolean isDetailPage(String url) {
		for (String regex : regexs) {
			if (url.matches(regex)) {
				return true;
			}
		}
		return false;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		NewsMeta news=new NewsMeta();
		String htmltxt=urlMeta.getHtml();
		String url=urlMeta.getUrl();
		news.setUrl(url);
		Document doc=Jsoup.parse(htmltxt);
		String title=null;
		String content=null;
		String author=null;
		Long date=null;

		title=doc.select("td.hong20").text();
		if(date==null){
			date=DateUtils.matchDate(doc.select("table.lan41").text());
		}

		content=HtmlCleaner.getContentHtml(url,doc.select("table.z1401"));

		/*
		 * 作者
		 */
		String authorStr=null;
		authorStr=doc.select("td[height=25]").text();
		author=StringUtils.substringBefore(authorStr, "[");


		news.setContent(content);
		news.setAuthor(author);
		news.setTitle(title);
		news.setClickNum(null);
		news.setDate(date);
		news.setType(1);
		return news;
	}

	public static void main(String[] args) {
		CnpcNewsAnalyse a=new CnpcNewsAnalyse();
		String url="http://news.cnpc.com.cn/system/2014/01/17/001467594.shtml";
		System.out.println(DoMainUtils.GetDomainName(url));
		UrlMeta meta=CrawlHTML.responseToURL(url);
		NewsMeta parserHtml=a.parserHtml(meta);
		System.out.println(parserHtml);
	}

	
	public NewsMeta Update(NewsMeta meta) {
		return null;
	}
	
	public boolean isNeedUpdate(){
		return false;
	}
}
